package cqrtplm.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.annotion.CustomListenerField;
import com.hustcad.plm.pdm.workflow.model.constant.ProcessConstant;
import cqrtplm.service.RTChangeNoticeService;
import cqrtplm.service.SetRelatePRECRStatusService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;

@Slf4j
@CustomListener(category = "润通", description = "设置更改通告关联的问题报告的状态")
public class RTSetRelatePRECRStatusListener implements ExecutionListener {
    @CustomListenerField(description = "PR目标生命周期状态", defaultValue = "", required = true)
    private FixedValue PRStatus;
    @CustomListenerField(description = "ECR目标生命周期状态", defaultValue = "", required = true)
    private FixedValue ECRStatus;
    @Override
    public void notify(DelegateExecution execution) {
        long start = System.currentTimeMillis();
        String workFlowContainerId = execution.getProcessInstanceBusinessKey();
        log.info("----------润通----------设置更改通告关联的问题报告的状态------workFlowContainerId:{}----->>>",
                 workFlowContainerId);
        if (StringUtils.isBlank(workFlowContainerId)) {
            workFlowContainerId = execution.getVariable(ProcessConstant.WORK_FLOW_CONTAINER_ID).toString();
        }
        SetRelatePRECRStatusService precStatusService = SpringUtil.getBean(SetRelatePRECRStatusService.class);
        try {
            precStatusService.SetRelatePRECRStatus(workFlowContainerId,PRStatus.getExpressionText(),ECRStatus.getExpressionText());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        log.info("----------同步完成：{}---->>>", System.currentTimeMillis() - start);
    }


}
